package com.pkk.cloud.support.oauth2.config.handle;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pkk.components.rpc.response.util.ResponseUtil;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

/**
 * @Description: 退出成功处理器
 * @Author: peikunkun
 * @Date: 2020/3/19 0019 下午 2:40
 */
@Slf4j
public class Oauth2LogoutSuccessHandler implements LogoutSuccessHandler {

  @Autowired
  private ObjectMapper objectMapper;

  /**
   * @Description: 退出成功之后设置通用的响应参数
   * @Param: request
   * @Param response
   * @Param authentication
   * @return: void
   * @Author: peikunkun
   * @Date: 2020/3/19 0019 下午 2:39
   */
  @Override
  public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
      Authentication authentication) throws IOException, ServletException {
    response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
    response.setStatus(HttpServletResponse.SC_OK);
    response.getWriter().write(objectMapper.writeValueAsString(ResponseUtil.success(null)));
  }
}
